// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
      "openid": ''
  },
  onLoad: function(options) {
    wx.login({
        success (res) {
            var that = this;
          if (res.code) {
              console.log("code:",res.code)
            //发起网络请求
            wx.request({
              url: 'http://localhost:8080/api/wechat/code2Session?jsCode='+res.code,
              data: {
                code: res.code
              },
            
              method: "POST",
              success: function (res) {
                  console.log(res.data.data);
                  // openid
                  console.log(res.data.data.openid);
                  wx.setStorageSync("openid",res.data.data.openid)
              }
            })
          } else {
            console.log('登录失败！' + res.errMsg)
          }
        }
      })
  },
  // 订阅消息(需要在微信小程序后台选用模板并配置好)
  requestSubscribeMessage() {
    wx.requestSubscribeMessage({
        tmplIds: ['4z5sVBmqu0f-98Z9UMzXlk4gkrziH_bbd7YvWY1pNDE'],
        success(res) {
            console.log("订阅成功")

            //发起网络请求
            wx.request({
                url: 'http://localhost:8080/api/wechat/sendSubscribeMsg',
                data: {
                    name: "张三",
                    phone: "18539930787",
                    openid: wx.getStorageSync("openid")
                },
              
                method: "POST",
                success: function (res) {
                    console.log(res.data.data);
                
                }
              })
        }
    })
},
})
